# 构建自己的centos8
FROM centos/systemd:latest

# 配置yum源
RUN cd /etc/yum.repos.d/ \
    && sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* \
    && sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* \
    && yum clean all && yum makecache

# 安装wget用于下载yum源配置文件
RUN yum install -y wget

# 备份原来的yum源配置文件（可选，但更安全，便于后续可能的恢复）
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$(date +%Y%m%d%H%M%S)

# 下载阿里云的CentOS 7的yum源配置文件
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 清理yum缓存并重新生成
RUN yum clean all && yum makecache

# 安装一些常用工具，比如net-tools（包含ifconfig等命令）、vim编辑器等，按需修改此处软件包列表
RUN yum install -y net-tools vim

# 安装 开发环境
RUN yum install -y openssh-server xauth xorg-x11-apps

# 生成 SSH 密钥
RUN ssh-keygen -A

# 修改 SSH 配置文件以允许 root 登录和 X11 转发
RUN sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#X11Forwarding no/X11Forwarding yes/' /etc/ssh/sshd_config
RUN sed -i 's/#X11DisplayOffset 10/X11DisplayOffset 10/' /etc/ssh/sshd_config
RUN sed -i 's/#X11UseLocalhost yes/X11UseLocalhost no/' /etc/ssh/sshd_config

# 创建 root 用户的密码（可选，但建议设置）
RUN echo 'root:123456' | chpasswd

# 安装 Docker
RUN yum install -y yum-utils \
    && yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo \
    && yum install -y docker-ce docker-ce-cli containerd.io

# 创建 .Xauthority 文件
RUN touch /root/.Xauthority && chmod 644 /root/.Xauthority

# 启用systemd
ENV container=docker
RUN (cd /lib/systemd/system/sysinit.target.wants/ || exit 0; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
    rm -f /lib/systemd/system/multi-user.target.wants/*; \
    rm -f /etc/systemd/system/*.wants/*; \
    rm -f /lib/systemd/system/local-fs.target.wants/*; \
    rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
    rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
    rm -f /lib/systemd/system/basic.target.wants/*; \
    rm -f /lib/systemd/system/anaconda.target.wants/*;

# 设置默认启动目标
CMD ["/usr/sbin/init"]

# 确保 SSH 服务在容器启动时自动启动
RUN systemctl enable sshd

# 确保 Docker 服务在容器启动时自动启动
RUN systemctl enable docker

# 添加 Docker 镜像加速配置
RUN mkdir -p /etc/docker
RUN echo '{ "registry-mirrors": ["https://docker.1panel.dev","https://dockerpull.org","https://docker.unsee.tech","https://docker.m.daocloud.io","https://docker.linkedbus.com","https://docker.xuanyuan.me","https://docker.rainbond.cc","https://docker.udayun.com"] }' > /etc/docker/daemon.json
